package com.tanhua.server.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.common.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
@Slf4j
@Service
public class UserService {
    @Autowired
    private RestTemplate restTemplate;
    private  static final ObjectMapper MAPPER=new ObjectMapper();
    @Value("${tanhua.sso.url}")
    private String ssoUrl;
    //通过SSO的rest接口查询
    public User queryUserByToken(String token) {
        String url=ssoUrl+"/user/"+token;
        try {
            String data = this.restTemplate.getForObject(url, String.class);
            if(StringUtils.isEmpty(data)){
                return null;
            }
            return MAPPER.readValue(data,User.class);
        } catch (Exception e) {
            log.error("校验token出错，token="+token,e);
        }
        return null;

    }
}
